<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">

      <el-form-item label="选择小区">
        <el-select v-model="form.communityId" clearable style="width: 400px;">
          <el-option
            v-for="community in communityList"
            :key="community.id"
            :label="community.name"
            :value="community.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="输入楼栋号">
        <el-input v-model="form.number" />
      </el-form-item>

      <el-form-item label="选择楼栋状态">
        <el-select v-model="form.statusId" clearable style="width: 400px;">
          <el-option :value="0" label="空闲中" />
          <el-option :value="1" label="使用中" />
        </el-select>
      </el-form-item>

      <el-form-item label="业主名称">
        <el-input v-model="form.ownerName" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认提交</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addRow } from '@/api/building'
import { validSubmitform } from '@/utils/validate'
import { getCommunityList } from '@/api/community'

export default {
  data() {
    return {
      form: {
        communityId: '',
        number: '',
        statusId: '',
        ownerName: ''
      },
      communityList: []
    }
  },
  mounted() {
    this.fetchCommunityList()
  },
  methods: {
    fetchCommunityList() {
      getCommunityList().then(response => {
        this.communityList = response.data
        console.log(response.data)
      })
    },
    onReset() {
      this.form = {
        communityId: '',
        number: '',
        statusId: '',
        ownerName: ''
      }
      this.$message({
        message: '已重置',
        type: 'warning'
      })
    },
    onSubmit() {
      console.log('表单数据:', this.form)
      if (validSubmitform(this.form)) {
        addRow(this.form)
          .then(response => {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          })
      } else {
        this.$message({
          message: '请填写完整信息',
          type: 'warning'
        })
      }
    }

  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

